package com.quick.develop.business.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.quick.develop.framework.common.annotation.CreateForm;
import com.quick.develop.framework.common.annotation.DetailForm;
import com.quick.develop.framework.common.annotation.QueryTable;
import com.quick.develop.framework.common.annotation.UpdateForm;
import lombok.AllArgsConstructor;
import com.quick.develop.framework.common.utils.PageResult;
import com.quick.develop.framework.mybatis.service.impl.BaseServiceImpl;
import com.quick.develop.business.convert.EnvironmentalReminderLogConvert;
import com.quick.develop.business.entity.EnvironmentalReminderLogEntity;
import com.quick.develop.business.query.EnvironmentalReminderLogQuery;
import com.quick.develop.business.vo.EnvironmentalReminderLogVO;
import com.quick.develop.business.dao.EnvironmentalReminderLogDao;
import com.quick.develop.business.service.EnvironmentalReminderLogService;
import com.quick.develop.form.utils.ExcelUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 短信提醒日志
 *
 * @author GJH 1906559468@qq.com
 * @since 1.0.0 2024-01-27
 */
@Service
@AllArgsConstructor
public class EnvironmentalReminderLogServiceImpl extends BaseServiceImpl<EnvironmentalReminderLogDao, EnvironmentalReminderLogEntity> implements EnvironmentalReminderLogService {

    @QueryTable
    @Override
    public PageResult<EnvironmentalReminderLogVO> page(EnvironmentalReminderLogQuery query) {
        IPage<EnvironmentalReminderLogEntity> page = baseMapper.selectPage(getPage(query), getWrapperOptional(query));

        return new PageResult<>(EnvironmentalReminderLogConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private LambdaQueryWrapper<EnvironmentalReminderLogEntity> getWrapper(EnvironmentalReminderLogQuery query) {
        LambdaQueryWrapper<EnvironmentalReminderLogEntity> wrapper = Wrappers.lambdaQuery();
        return wrapper;
    }

    private LambdaQueryWrapper<EnvironmentalReminderLogEntity> getWrapperOptional(EnvironmentalReminderLogQuery query) {
        LambdaQueryWrapper<EnvironmentalReminderLogEntity> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(!StrUtil.isBlankIfStr(query.getId()), EnvironmentalReminderLogEntity::getId, query.getId());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getBusinessKey()), EnvironmentalReminderLogEntity::getBusinessKey, query.getBusinessKey());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getResponsiblePersonalName()), EnvironmentalReminderLogEntity::getResponsiblePersonalName, query.getResponsiblePersonalName());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getResponsiblePersonalCode()), EnvironmentalReminderLogEntity::getResponsiblePersonalCode, query.getResponsiblePersonalCode());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getResponsibleDepartmentName()), EnvironmentalReminderLogEntity::getResponsibleDepartmentName, query.getResponsibleDepartmentName());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getResponsibleDepartmentCode()), EnvironmentalReminderLogEntity::getResponsibleDepartmentCode, query.getResponsibleDepartmentCode());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getCategory()), EnvironmentalReminderLogEntity::getCategory, query.getCategory());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getRemindDate()), EnvironmentalReminderLogEntity::getRemindDate, query.getRemindDate());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getSendDate()), EnvironmentalReminderLogEntity::getSendDate, query.getSendDate());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getSendStatus()), EnvironmentalReminderLogEntity::getSendStatus, query.getSendStatus());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getCreatedUserCode()), EnvironmentalReminderLogEntity::getCreatedUserCode, query.getCreatedUserCode());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getCreatedUserName()), EnvironmentalReminderLogEntity::getCreatedUserName, query.getCreatedUserName());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getCreatedTime()), EnvironmentalReminderLogEntity::getCreatedTime, query.getCreatedTime());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getUpdatedUserCode()), EnvironmentalReminderLogEntity::getUpdatedUserCode, query.getUpdatedUserCode());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getUpdatedUserName()), EnvironmentalReminderLogEntity::getUpdatedUserName, query.getUpdatedUserName());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getUpdatedTime()), EnvironmentalReminderLogEntity::getUpdatedTime, query.getUpdatedTime());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getDelFlag()), EnvironmentalReminderLogEntity::getDelFlag, query.getDelFlag());
            wrapper.like(!StrUtil.isBlankIfStr(query.getIdLike()), EnvironmentalReminderLogEntity::getId, query.getIdLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getBusinessKeyLike()), EnvironmentalReminderLogEntity::getBusinessKey, query.getBusinessKeyLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getResponsiblePersonalNameLike()), EnvironmentalReminderLogEntity::getResponsiblePersonalName, query.getResponsiblePersonalNameLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getResponsiblePersonalCodeLike()), EnvironmentalReminderLogEntity::getResponsiblePersonalCode, query.getResponsiblePersonalCodeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getResponsibleDepartmentNameLike()), EnvironmentalReminderLogEntity::getResponsibleDepartmentName, query.getResponsibleDepartmentNameLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getResponsibleDepartmentCodeLike()), EnvironmentalReminderLogEntity::getResponsibleDepartmentCode, query.getResponsibleDepartmentCodeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getCategoryLike()), EnvironmentalReminderLogEntity::getCategory, query.getCategoryLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getRemindDateLike()), EnvironmentalReminderLogEntity::getRemindDate, query.getRemindDateLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getSendDateLike()), EnvironmentalReminderLogEntity::getSendDate, query.getSendDateLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getSendStatusLike()), EnvironmentalReminderLogEntity::getSendStatus, query.getSendStatusLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getCreatedUserCodeLike()), EnvironmentalReminderLogEntity::getCreatedUserCode, query.getCreatedUserCodeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getCreatedUserNameLike()), EnvironmentalReminderLogEntity::getCreatedUserName, query.getCreatedUserNameLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getCreatedTimeLike()), EnvironmentalReminderLogEntity::getCreatedTime, query.getCreatedTimeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getUpdatedUserCodeLike()), EnvironmentalReminderLogEntity::getUpdatedUserCode, query.getUpdatedUserCodeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getUpdatedUserNameLike()), EnvironmentalReminderLogEntity::getUpdatedUserName, query.getUpdatedUserNameLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getUpdatedTimeLike()), EnvironmentalReminderLogEntity::getUpdatedTime, query.getUpdatedTimeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getDelFlagLike()), EnvironmentalReminderLogEntity::getDelFlag, query.getDelFlagLike()).or();
        return wrapper;
    }

    @CreateForm
    @Override
    public void save(EnvironmentalReminderLogVO vo) {
        EnvironmentalReminderLogEntity entity = EnvironmentalReminderLogConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @UpdateForm
    @Override
    public void update(EnvironmentalReminderLogVO vo) {
        EnvironmentalReminderLogEntity entity = EnvironmentalReminderLogConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<String> idList) {
        removeByIds(idList);
    }

    @Override
    public EnvironmentalReminderLogVO getEnvironmentalReminderLogVO(String id) {
    EnvironmentalReminderLogEntity entity = getById(id);
        return EnvironmentalReminderLogConvert.INSTANCE.convert(entity);
    }

    @DetailForm
    @Override
    public EnvironmentalReminderLogVO getEnvironmentalReminderLogByBusinessKey(String businessKey) {
        EnvironmentalReminderLogEntity entity = baseMapper.selectOne(new LambdaQueryWrapper<EnvironmentalReminderLogEntity>()
                .eq(EnvironmentalReminderLogEntity::getBusinessKey, businessKey));
        return EnvironmentalReminderLogConvert.INSTANCE.convert(entity);
    }

    @Override
    public List<EnvironmentalReminderLogVO> getListByKeys(EnvironmentalReminderLogQuery query) {
        return this.getListByKeys(query.getFieldName(), query.getCodes());
    }

    @Override
    public void dynamicExportExcel(EnvironmentalReminderLogQuery query) {
        List<EnvironmentalReminderLogEntity> list = this.list(getWrapperOptional(query));
        ExcelUtils.downloadExcel(EnvironmentalReminderLogEntity.class, list, "短信提醒日志导出");
    }

    @Override
    public void dynamicImportExcel(MultipartFile file) {
        ExcelUtils.execWriteDatabase(
                file,
                EnvironmentalReminderLogEntity.class,
                this::filterDynamicImport,
                this::overrideDynamicImport,
                this::updateBatchById,
                this::saveBatch
        );
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchSave(List<EnvironmentalReminderLogVO> list) {
        if (CollUtil.isNotEmpty(list)) {
            List<EnvironmentalReminderLogEntity> entities = list.stream().map(EnvironmentalReminderLogConvert.INSTANCE::convert).collect(Collectors.toList());
            saveBatch(entities);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchUpdateById(List<EnvironmentalReminderLogVO> list) {
        if (CollUtil.isNotEmpty(list)) {
            List<EnvironmentalReminderLogEntity> entities = list.stream().map(EnvironmentalReminderLogConvert.INSTANCE::convert).collect(Collectors.toList());
            updateBatchById(entities);
        }
    }

    public List<EnvironmentalReminderLogVO> getListByKeys(String key, Collection<Serializable> keys) {
        if (CollUtil.isNotEmpty(keys) && StrUtil.isNotEmpty(key)) {
            return Collections.emptyList();
        }
        return EnvironmentalReminderLogConvert.INSTANCE.convertList(list(Wrappers.query(EnvironmentalReminderLogEntity.class).in(key, keys)));
    }

    /**
    *
    * @param sFunction 函数式获取Key
    * @param keys 需要查询的列表
    * @return 返回数据列表
    */
    public List<EnvironmentalReminderLogEntity> getListByKeys(SFunction<EnvironmentalReminderLogEntity, ?> sFunction, Collection<Serializable> keys) {
        if (CollUtil.isNotEmpty(keys)) {
            return Collections.emptyList();
        }
        return list(Wrappers.lambdaQuery(EnvironmentalReminderLogEntity.class).in(sFunction, keys));
    }

    /**
     * @param list 解析的数据
     * @return 过滤以后的数据
     */
    public List<EnvironmentalReminderLogEntity> filterDynamicImport(List<EnvironmentalReminderLogEntity> list) {
        return list;
    }

    /**
     * @param list 解析的数据
     * @return 返回过滤的数据
     * @description 覆盖导入的数据
     */
    public List<EnvironmentalReminderLogEntity> overrideDynamicImport(List<EnvironmentalReminderLogEntity> list) {
        return Collections.emptyList();
    }
}